//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.spring.schedule_system.dengxiaojiao.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.prefs.BackingStoreException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

public class DefaultLyLog implements LyLog {
    public static volatile boolean ENABLE_DEV = false;
    public static String APPNAME;
    public static int SUBREQUESTLENGTH = 10240;
    private Logger log;
    private static final String LOG_NAME = "LY-LOGNAME";
    private static final String LOG_TYPE = "logtype";
    private static final String OPER_ID = "operid";
    private static final String METHOD = "method";
    private static final String PATH = "path";
    private static final String CQRS_FLAG = "cqrsFlag";
    private static final String SPILE_FLAG = "-";
    private static final String LOG_INFO = "loginfo";
    private static final String BIZ = "-biz";
    private static final String BIZ_PARAM = "biz";
    private static final String REQUEST = "-request";
    private static final String REQUEST_PARAM = "request";
    private static final String ERR = "-err";
    private static final String ERR_PARAM = "err";
    private static final String PFM = "-pfm";
    private static final String PFM_PARAM = "pfm";
    private static final String SPENDTIME = "spendtime";

    public DefaultLyLog() {
    }

    public DefaultLyLog(String name) {
        this.log = LoggerFactory.getLogger(name);
    }

    public DefaultLyLog(Class<?> clazz) {
        this.log = LoggerFactory.getLogger(clazz);
    }

    private String createLogName(LogBase logBase) {
        if (!StringUtils.hasText(logBase.getLogtype())) {
            return "1";
        } else {
            String logName;
            if (StringUtils.hasText(logBase.getPrefix())) {
                logName = logBase.getPrefix().concat("-").concat(logBase.getLogtype());
            } else {
                logName = APPNAME.concat("-").concat(logBase.getLogtype());
            }

            return logName;
        }
    }

    public void info(LogBase logBase) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", this.createLogName(logBase));
                this.log.info(getParams(logBase));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void warn(LogBase logBase) {
        if (this.log.isWarnEnabled()) {
            try {
                MDC.put("LY-LOGNAME", this.createLogName(logBase));
                this.log.warn(getParams(logBase));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void error(LogBase logBase) {
        if (this.log.isErrorEnabled()) {
            try {
                MDC.put("LY-LOGNAME", this.createLogName(logBase));
                this.log.error(getParams(logBase));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    private static String getParams(LogBase logBase) {
        JSONObject jsonObject = (JSONObject)JSON.toJSON(logBase);
        return jsonObject.toString();
    }

    public void info(String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-biz");
                this.log.info(this.getParams("biz", -1L, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void warn(String format, Object... args) {
        if (this.log.isWarnEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-biz");
                this.log.warn(this.getParams("biz", -1L, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void request(String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-request");
                this.log.info(this.getReqParams((String)null, (String)null, "request", -1L, format, true, SUBREQUESTLENGTH, (String)null, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void error(String format, Object... args) {
        if (this.log.isErrorEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-err");
                this.log.error(this.getParams("err", -1L, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void error(String msg, Throwable e) {
        if (this.log.isErrorEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-err");
                this.log.error(this.getParams("err", -1L, String.format("%s,堆栈信息:%s", msg, getStackTrace(e)), false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(Date startTime, Date endTime, String logInfo, Object... args) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(startTime);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(endTime);
        long spendTime = c2.getTimeInMillis() - c1.getTimeInMillis();
        this.pfmlog(spendTime, logInfo, args);
    }

    public void pfmlog(long spendTime, String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParams("pfm", spendTime, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    private String getReqParams(String method, String path, String logType, long spendTime, String format, boolean sub, int length, String cqrsFlag, Object... args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logtype", logType);
        jsonObject.put("method", method);
        jsonObject.put("path", path);
        jsonObject.put("cqrsFlag", cqrsFlag);
        if (spendTime >= 0L) {
            jsonObject.put("spendtime", spendTime);
        }



        return jsonObject.toString();
    }

    private String getParamsPfm(String logType, String method, String path, long spendTime, String format, boolean sub, int length, Object... args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logtype", logType);
        jsonObject.put("method", method);
        jsonObject.put("path", path);
        if (spendTime >= 0L) {
            jsonObject.put("spendtime", spendTime);
        }



        return jsonObject.toString();
    }

    private String getParams(String logType, long spendTime, String format, boolean sub, int length, Object... args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logtype", logType);
        if (spendTime >= 0L) {
            jsonObject.put("spendtime", spendTime);
        }



        return jsonObject.toString();
    }

    public static String getStackTrace(Throwable throwable) {
        if (ENABLE_DEV) {
            throwable.printStackTrace();
        }

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }

    public void info(String msg) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-biz");
                this.log.info(this.getParams("biz", -1L, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void warn(String msg) {
        if (this.log.isWarnEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-biz");
                this.log.warn(this.getParams("biz", -1L, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void error(String msg) {
        if (this.log.isErrorEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-err");
                this.log.error(this.getParams("err", -1L, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void request(String msg) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-request");
                this.log.info(this.getParams("request", -1L, msg, true, SUBREQUESTLENGTH));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(Date startTime, Date endTime, String msg) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(startTime);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(endTime);
        long spendTime = c2.getTimeInMillis() - c1.getTimeInMillis();
        this.pfmlog(spendTime, msg);
    }

    public void pfmlog(long spendTime, String msg) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParams("pfm", spendTime, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(long spendTime, String method, String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParamsPfm("pfm", method, (String)null, spendTime, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(long spendTime, String method, String msg) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParamsPfm("pfm", method, (String)null, spendTime, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(long spendTime, String method, String path, String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParamsPfm("pfm", method, path, spendTime, format, false, 0, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void pfmlog(long spendTime, String method, String path, String msg) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-pfm");
                this.log.info(this.getParamsPfm("pfm", method, path, spendTime, msg, false, 0));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void request(String method, String path, String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-request");
                this.log.info(this.getReqParams(method, path, "request", -1L, format, true, SUBREQUESTLENGTH, (String)null, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }

    public void request(String method, String path, String cqrsFlag, String format, Object... args) {
        if (this.log.isInfoEnabled()) {
            try {
                MDC.put("LY-LOGNAME", APPNAME + "-request");
                this.log.info(this.getReqParams(method, path, "request", -1L, format, true, SUBREQUESTLENGTH, cqrsFlag, args));
            } finally {
                MDC.remove("LY-LOGNAME");
            }
        }

    }
}
